﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Novacode;
using System.Diagnostics;
using GibberDoc.Classes.UniTech;
using System.IO;

namespace GibberDoc
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(txtPath.Text))
            {
                MessageBox.Show("Invalid folder path");
                return;
            }
            // Modify to suit your machine:

            RandomParagraphGenerator generator = new RandomParagraphGenerator(chkUseBusiness.Checked);
            
            // TODO: Add lockout to UI when creating DOCs

            this.Refresh();

            int docQuantity = int.Parse(txtDocuments.Text);
            tlProgressBar.Maximum = docQuantity;
            tlProgressBar.Value = 0;

            for (int i = 0; i < docQuantity; i++)
            {
                tlProgressBar.Value = i+1;
                Application.DoEvents();

                string fileName = txtPath.Text.EnsureTrailingSlash() + generator.DocName + ".docx";

                // Create a document in memory:
                var doc = DocX.Create(fileName);

                generator.ClearBuilder();
                generator.AddContentParagraphs(int.Parse(txtParagraphs.Text), int.Parse(txtMinSentences.Text), int.Parse(txtMaxSentences.Text));

                // Insert a paragrpah:
                doc.InsertParagraph(generator.Content);

                // Save to the output directory:
                doc.Save();
            }

            //MessageBox.Show(string.Format("Operation complex {0} documents created in {1}", docQuantity, txtPath.Text));
        }

        private void frmMain_Load(object sender, EventArgs e)
        {

        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            fDlgSelectFolder.Description = "Select the document output folder destination.";
            fDlgSelectFolder.RootFolder = Environment.SpecialFolder.MyComputer;
            fDlgSelectFolder.ShowNewFolderButton = true;

            DialogResult result = fDlgSelectFolder.ShowDialog();

            if (result == DialogResult.OK)
            {
                txtPath.Text = fDlgSelectFolder.SelectedPath;
            }
        }
    }
}
